﻿using System;
using System.Collections.Generic;
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using MyHotel_DTO;
using MyHotel_Common;

namespace MyHotel_DAO
{
    public class KhachHangDAO : DAO
    {
        protected override object createObjectFromDatarow(DataRow row)
        {
            KhachHang khachhang = new KhachHang();
            khachhang.MaKhachHang = row["MAKH"].ToString();
            khachhang.TenKhachHang = row["TenKH"].ToString();
            khachhang.CMND = row["CMND"].ToString();
            khachhang.DiaChi = row["DiaChi"].ToString();
            return khachhang;
        }
        
        public List<KhachHang> getList()
        {
            string nameProc = "SP_khachhang_GETLIST";
           List<KhachHang> listkhachhang= new List<KhachHang>();
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);
            
            return getListFromDataTable<KhachHang>(dt); 
        }
        public int themKhachHang(KhachHang khachhang)
        {
            string nameProc = "them_khachhang";
            SqlParameter[] para = new SqlParameter[3];
            para[0] = new SqlParameter("@tenkh", khachhang.TenKhachHang);
            para[1] = new SqlParameter("@cmnd", khachhang.CMND);
            para[2] = new SqlParameter("@diachi", khachhang.DiaChi);
            if (DataProvider.executeStoreProcedureNonQuery(nameProc, para) == 1)
                return getIDMax();
            return 0;
        }
 
        public int getIDMax()
        {
            string nameProc = "idmax_khachhang"; 
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null); 
            return Convert.ToInt32(dt.Rows[0]["idmax"]);
        }
    }
}
